/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-03 10:15:52
 * @LastEditTime: 2023-01-30 15:40:25
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import {
  SET_SELECT_FOND,
  SET_SELECT_AT,
  SET_SELECT_KEY,
  GET_CURRENT,
  DEL_FATLIST,
  ADD_FATLIST,
  ADD_TAFLIST,
  DEL_TATLIST,
  EDIT_FATLIST,
  EDIT_TAFLIST,
  SAVE_HIERACHY,
  SAVE_BASE
} from '../constants';
import { MappingRelationReducer } from '../reducers/MappingRelationReducer';
import { message, Modal } from 'antd';

const initialState = {
  // 当前选中的项目
  selectFond: {},
  selectAT: {},
  current: null,
  FATList: [],
  TAFList: [],
  TATList: [{ id: 2 }],
  selectItems: [],
  hierachy: [],
  base: {}
};
const MappingRelationContext = createContext();
const MappingRelationContextProvider = (props) => {
  const [state, dispatch] = useReducer(MappingRelationReducer, initialState);
  const actions = {
    /**
     * @description: 设置选中全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },
    setSelectKey(data) {
      dispatch({ type: SET_SELECT_KEY, data });
    },
    /* 获取选择类型 */
    getCurrent(data) {
      dispatch({ type: GET_CURRENT, data });
    },
    /* 获取散文件与档案类型列表 */
    getFATList(prams) {
      // dispatch({ type: GET_FATLIST });
    },
    /* 获取档案类型与档案类型列表 */
    getTATList(prams) {},
    /* 新增散文件与档案类型 */
    addFAT(data, type) {
      // addBoxNorms(data).then((res) => {
      //   const { data } = res;
      //   console.log(data);
      //   dispatch({ type: ADD_FATLIST, data });
      // });
      if (type === 1) {
        dispatch({ type: ADD_FATLIST, data });
      } else {
        dispatch({ type: ADD_TAFLIST, data });
      }
    },
    /* 删除散文件与档案类型 */
    delFAT(id, title = '确认删除该映射？') {
      // Modal.confirm({
      //   title: '删除',
      //   content: title,
      //   onOk() {
      //     const ids = id instanceof Array ? id.join(',') : id;
      //     delBoxNorms(ids).then((res) => {
      //       message.success('删除成功');
      //       const data = res.data.map((item) => parseInt(item, 10));
      //       dispatch({ type: DEL_FATLIST, ids: data });
      //     });
      //   },
      //   onCancel() {
      //     console.log('Cancel');
      //   }
      // });
      dispatch({ type: DEL_FATLIST, id });
    },
    delTAT(id, title = '确认删除该映射？') {
      // Modal.confirm({
      //   title: '删除',
      //   content: title,
      //   onOk() {
      //     const ids = id instanceof Array ? id.join(',') : id;
      //     delBoxNorms(ids).then((res) => {
      //       message.success('删除成功');
      //       const data = res.data.map((item) => parseInt(item, 10));
      //       dispatch({ type: DEL_TATLIST, ids: data });
      //     });
      //   },
      //   onCancel() {
      //     console.log('Cancel');
      //   }
      // });
      dispatch({ type: DEL_TATLIST, id });
    },
    /* 修改散文件与档案类型映射 */
    editFAT(query, type) {
      // return new Promise((resolve, reject) => {
      //   editFAT(query)
      //     .then((res) => {
      //       const { data } = res;
      //       dispatch({ type: EDIT_FATLIST, data, id: data.id });
      //       resolve(data);
      //     })
      //     .catch((err) => reject(err));
      // });
      if (type === 1) {
        dispatch({ type: EDIT_FATLIST, data: query, id: query.id });
      } else {
        dispatch({ type: EDIT_TAFLIST, data: query, id: query.id });
      }
    },
    /* 保存层级匹配设置 */
    saveHierachy(data) {
      dispatch({ type: SAVE_HIERACHY, data });
    },
    /* 保存基础匹配配置 */
    saveBase(data) {
      dispatch({ type: SAVE_BASE, data });
    }
  };
  useEffect(() => {
    if (state.selectFond?.id) {
      actions.getArchiveTypeList(state.selectFond.id);
      // actions.getArchiveTypeList({ id: 107 });
    }
  }, [state.selectFond]);
  return (
    <MappingRelationContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </MappingRelationContext.Provider>
  );
};
export const useMappingRelationContext = () => {
  return useContext(MappingRelationContext);
};
export { MappingRelationContext, MappingRelationContextProvider };
